package com.pioneer.gui.invoice.view;

import com.pioneer.gui.invoice.view.InvoicePanel;
import com.pioneer.gui.invoice.model.InvoiceModel;
import com.pioneer.gui.invoice.util.Constants;
//import com.pioneer.gui.invoice.controller.AddressSelection;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.Box;
import javax.swing.JLabel;

import java.util.Vector;

public class AddressPanel
extends JPanel
implements Constants
{
	private final int ADDRESS_MAX_LENGTH=30;
	private final int PHONE_LENGTH=5;
	private final int ADDRESS_ROWS=3;
	private InvoiceModel model;
	
	public JComboBox billToDropDown;
	public JTextField billToAddressStreet;
	public JTextField billToAddressCity;
	public JTextField billToAddressState;
	public JTextField billToAddressZip;
	public JTextField billToAddressPhone;
	public JTextField billToAddressFax;
	
	public JComboBox shipToDropDown;
	public JTextField shipToAddressStreet;
	public JTextField shipToAddressCity;
	public JTextField shipToAddressState;
	public JTextField shipToAddressZip;
	public JTextField shipToAddressPhone;
	public JTextField shipToAddressFax;
	
	public AddressPanel(InvoiceModel model)	
	{
		this.model=model;
		this.setLayout(new GridLayout(1,2));
		
		//JPanel billPanel=new JPanel();
		//billPanel.setLayout(new BorderLayout());		
		
		JPanel billAddressPanel=new JPanel();
		billAddressPanel.setLayout(new GridLayout(4,1));
		billToDropDown=new JComboBox(model.getBillToContact());
		billToDropDown.addActionListener(new AddressSelection(BILL,billToDropDown,this.model));
		billAddressPanel.add(billToDropDown);		
		
		billToAddressStreet=new JTextField(model.getBillAddress(),null,ADDRESS_MAX_LENGTH);
		billAddressPanel.add(billToAddressStreet);
		JPanel billAddressLocationPanel=new JPanel();
		//billAddressLocationPanel.setLayout(new GridLayout(1,3));
		billAddressLocationPanel.setLayout(new BorderLayout());
		billToAddressCity=new JTextField(model.getBillCity(),null,ADDRESS_MAX_LENGTH);
		billAddressLocationPanel.add(billToAddressCity,BorderLayout.CENTER);		
		JPanel billStateZipPanel=new JPanel();
		billStateZipPanel.setLayout(new GridLayout(1,2));
		billToAddressState=new JTextField(model.getBillState(),null,2);
		billStateZipPanel.add(billToAddressState);
		billToAddressZip=new JTextField(model.getBillZip(),null,5);
		billStateZipPanel.add(billToAddressZip);
		billAddressLocationPanel.add(billStateZipPanel,BorderLayout.EAST);
		
		billAddressPanel.add(billAddressLocationPanel);
		
		JPanel billAddressPhoneFaxPanel=new JPanel();
		billAddressPhoneFaxPanel.setLayout(new GridLayout(1,2));
		
		JPanel billAddressPhonePanel=new JPanel();
		billAddressPhonePanel.setLayout(new BorderLayout());
		billAddressPhonePanel.add(new JLabel("Phone ",SwingConstants.RIGHT),BorderLayout.WEST);		
		billToAddressPhone=new JTextField(model.getBillPhone(),null,PHONE_LENGTH);
		billAddressPhonePanel.add(billToAddressPhone,BorderLayout.CENTER);
		billAddressPhoneFaxPanel.add(billAddressPhonePanel);
		
		JPanel billAddressFaxPanel=new JPanel();
		billAddressFaxPanel.setLayout(new BorderLayout());
		billAddressFaxPanel.add(new JLabel("Fax ",SwingConstants.RIGHT),BorderLayout.WEST);		
		billToAddressFax=new JTextField(model.getBillFax(),null,PHONE_LENGTH);
		billAddressFaxPanel.add(billToAddressFax,BorderLayout.CENTER);
		billAddressPhoneFaxPanel.add(billAddressFaxPanel);
		
		billAddressPanel.add(billAddressPhoneFaxPanel);		
		
		//billPanel.add(new LabelPanel(new JLabel("Bill To"),billAddressPanel),BorderLayout.CENTER);
		//this.add(billPanel);
		this.add(new LabelPanel(new JLabel("Bill To"),billAddressPanel),BorderLayout.CENTER);
		
		//this.add(new Box(BoxLayout.X_AXIS).createVerticalStrut(2));
		
		//JPanel shipPanel=new JPanel();
		//shipPanel.setLayout(new BorderLayout());		
		JPanel shipAddressPanel=new JPanel();
		shipAddressPanel.setLayout(new GridLayout(4,1));
		shipToDropDown=new JComboBox(model.getShipToContact());
		shipToDropDown.addActionListener(new AddressSelection(SHIP,shipToDropDown,this.model));
		shipAddressPanel.add(shipToDropDown);
		
		shipToAddressStreet=new JTextField(model.getShipAddress(),null,ADDRESS_MAX_LENGTH);
		shipAddressPanel.add(shipToAddressStreet);
		JPanel shipAddressLocationPanel=new JPanel();
		//shipAddressLocationPanel.setLayout(new GridLayout(1,3));
		shipAddressLocationPanel.setLayout(new BorderLayout());
		shipToAddressCity=new JTextField(model.getShipCity(),null,ADDRESS_MAX_LENGTH);
		shipAddressLocationPanel.add(shipToAddressCity,BorderLayout.CENTER);		
		JPanel shipStateZipPanel=new JPanel();
		shipStateZipPanel.setLayout(new GridLayout(1,2));
		shipToAddressState=new JTextField(model.getShipState(),null,2);
		shipStateZipPanel.add(shipToAddressState);
		shipToAddressZip=new JTextField(model.getShipZip(),null,5);
		shipStateZipPanel.add(shipToAddressZip);
		shipAddressLocationPanel.add(shipStateZipPanel,BorderLayout.EAST);
		
		shipAddressPanel.add(shipAddressLocationPanel);
		
		JPanel shipAddressPhoneFaxPanel=new JPanel();
		shipAddressPhoneFaxPanel.setLayout(new GridLayout(1,2));
		
		JPanel shipAddressPhonePanel=new JPanel();
		shipAddressPhonePanel.setLayout(new BorderLayout());
		shipAddressPhonePanel.add(new JLabel("Phone ",SwingConstants.RIGHT),BorderLayout.WEST);		
		shipToAddressPhone=new JTextField(model.getShipPhone(),null,PHONE_LENGTH);
		shipAddressPhonePanel.add(shipToAddressPhone,BorderLayout.CENTER);
		shipAddressPhoneFaxPanel.add(shipAddressPhonePanel);
		
		JPanel shipAddressFaxPanel=new JPanel();
		shipAddressFaxPanel.setLayout(new BorderLayout());
		shipAddressFaxPanel.add(new JLabel("Fax ",SwingConstants.RIGHT),BorderLayout.WEST);		
		shipToAddressFax=new JTextField(model.getShipFax(),null,PHONE_LENGTH);
		shipAddressFaxPanel.add(shipToAddressFax,BorderLayout.CENTER);
		shipAddressPhoneFaxPanel.add(shipAddressFaxPanel);
		
		shipAddressPanel.add(shipAddressPhoneFaxPanel);
		
		//shipPanel.add(new LabelPanel(new JLabel("Ship To"),shipAddressPanel),BorderLayout.CENTER);
		this.add(new LabelPanel(new JLabel("Ship To"),shipAddressPanel),BorderLayout.CENTER);
		//this.add(shipPanel);
	}
	
	class BillAddressAdapter
	implements ActionListener
	{
		String[] addressInfo;
		
		BillAddressAdapter()
		{
			addressInfo=new String[6];
		}
		
		public void actionPerformed(ActionEvent e)
		{			
			/*addressInfo[InvoiceModel.ADDRESS]= billToAddressStreet.getText();
			addressInfo[InvoiceModel.CITY]= billToAddressCity.getText();
			addressInfo[InvoiceModel.STATE]= billToAddressState.getText();
			addressInfo[InvoiceModel.ZIP]= billToAddressZip.getText();
			addressInfo[InvoiceModel.PHONE]= billToAddressPhone.getText();
			addressInfo[InvoiceModel.FAX]= billToAddressFax.getText();
			model.setBillInfo(addressInfo);*/
		}
	}
	
	class ShipAddressAdapter
	implements ActionListener
	{
		String[] addressInfo;
		
		ShipAddressAdapter()
		{
			addressInfo=new String[6];
		}
		
		public void actionPerformed(ActionEvent e)
		{
			/*
			addressInfo[InvoiceModel.ADDRESS]= shipToAddressStreet.getText();
			addressInfo[InvoiceModel.CITY]= shipToAddressCity.getText();
			addressInfo[InvoiceModel.STATE]= shipToAddressState.getText();
			addressInfo[InvoiceModel.ZIP]= shipToAddressZip.getText();
			addressInfo[InvoiceModel.PHONE]= shipToAddressPhone.getText();
			addressInfo[InvoiceModel.FAX]= shipToAddressFax.getText();
			model.setShipInfo(addressInfo);
			*/
		}
	}
	
	class AddressSelection
	implements ActionListener,Constants
	{
		private String addressType=new String();
		String[] addressInfo;
		private JComboBox contactListDropDown;
		private InvoiceModel model;
		private AddressPanel addressPanel;
		
		public AddressSelection(String addressType,JComboBox contactList,InvoiceModel model)
		{
			/*
			this.addressInfo=new String[6];
			this.addressType=addressType;
			this.contactListDropDown=contactList;
			this.model=model;
			this.addressPanel=addressPanel;*/
		}
	
		public void actionPerformed(ActionEvent e)
		{
			/*VectorList contactList=(VectorList)this.contactListDropDown.getModel();
			Object obj=contactList.getDatabaseKey();
			if (obj==null)
				return;
			int contactId=((Integer)obj).intValue();
			InvoiceDAO dao=new InvoiceDAO();
			String[] addressInfo=dao.findAddressByContactId(this.model.getCusId(),addressType,contactId );
			if (addressType.equals(BILL))
			{
				addressPanel.billToAddressStreet.setText(addressInfo[InvoiceModel.ADDRESS]);
				addressPanel.billToAddressCity.setText(addressInfo[InvoiceModel.CITY]);
				addressPanel.billToAddressState.setText(addressInfo[InvoiceModel.STATE]);
				addressPanel.billToAddressZip.setText(addressInfo[InvoiceModel.ZIP]);
				addressPanel.billToAddressPhone.setText(addressInfo[InvoiceModel.PHONE]);
				addressPanel.billToAddressFax.setText(addressInfo[InvoiceModel.FAX]);
				model.setBillInfo(addressInfo);
			}
			else if (addressType.equals(SHIP))
			{
				addressPanel.shipToAddressStreet.setText(addressInfo[InvoiceModel.ADDRESS]);
				addressPanel.shipToAddressCity.setText(addressInfo[InvoiceModel.CITY]);
				addressPanel.shipToAddressState.setText(addressInfo[InvoiceModel.STATE]);
				addressPanel.shipToAddressZip.setText(addressInfo[InvoiceModel.ZIP]);
				addressPanel.shipToAddressPhone.setText(addressInfo[InvoiceModel.PHONE]);
				addressPanel.shipToAddressFax.setText(addressInfo[InvoiceModel.FAX]);
				model.setShipInfo(addressInfo);
			}*/
		}
	}
	
	
}

	
